<template>
	<view>
		<u--form :model="enterprise" labelWidth="auto" ref="form1">
			<u-form-item label="企业名称: " prop="enterprise.name" borderBottom ref="item1">
				<u--input disabled v-model="enterprise.name" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="工商注册号: " prop="enterprise.registrationNo" borderBottom ref="item1">
				<u--input disabled v-model="enterprise.registrationNo" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="企业电话: " prop="enterprise.enPhone" borderBottom ref="item2">
				<u--input v-model="enterprise.enPhone" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="负责人: " prop="enterprise.chargePerson" borderBottom ref="item2">
				<u--input v-model="enterprise.chargePerson" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="负责人电话: " prop="enterprise.chargePhone" borderBottom ref="item3">
				<u--input v-model="enterprise.chargePhone" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="地址: " prop="enterprise.address" borderBottom ref="item3">
				<u--input v-model="enterprise.address" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="企业性质" prop="enterprise.natureId" borderBottom @tap="showSex = true;" ref="item1">
				<u--input v-model="enterprise.tbEnNature.name" border="bottom" inputAlign="center" disabled
					disabledColor="#ffffff" placeholder="请选择企业性质"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			<u-form-item label="企业描述: " prop="enterprise.desc" ref="item1">
				<u--textarea v-model="enterprise.desc" placeholder="请输入内容"></u--textarea>
			</u-form-item>
			<u-form-item label="企业图片: " ref="item1">
				<view>
					<image mode="aspectFill"
						style="display: inline-block; margin-right: 10rpx; width:100px; height:80px;"
						:src="item.photoUrl" v-for="(item,index) in enterprise.tbPhotoList" @click="delPhoto(item.id)">
					</image>
					<u-upload width="100px" height="80px" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="10"></u-upload>
				</view>
			</u-form-item>

		</u--form>
		<u-action-sheet :show="showSex" :actions="natureList" title="请选择企业性质" @close="showSex = false"
			@select="selectNature">
		</u-action-sheet>
		<view>
			<u-button type="primary" @click="updateEnterprise()" icon="plus">保存信息</u-button>
			<u-button type="error" customStyle="margin-top:15px" @tap="delEnterprise()" icon="info">注销企业</u-button>
		</view>
	</view>
</template>

<script>
	import {
		findById,
		findAllNature,
		delPhoto,
		updateEnterprise,
		delEnterprise
	} from '@/api/enterprise.js'
	import {
		Toast
	} from '../../common/common.js'
	export default {
		data() {
			return {
				enterprise: {},
				natureList: [],
				showSex: false,
				fileList1: [],
			}
		},
		methods: {
			findById() {
				findById({
					"id": this.enterprise.id
				}).then(res => {
					this.enterprise = res.data.data
				})
			},
			findAllNature() {
				findAllNature({}).then(res => {
					this.natureList = res.data.data
				})
			},
			selectNature(nature) {
				// console.log(nature)
				this.enterprise.natureId = nature.id
				this.enterprise.tbEnNature = nature
			},
			delPhoto(id) {
				delPhoto({
					'enterpriseId': this.enterprise.id,
					'photoId': id
				}).then(res => {
					this.findById()
				})
			},


			//上传图片
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `http://localhost:18001/api/photo/upload/${this.enterprise.id}`,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			
			updateEnterprise(){
				updateEnterprise(this.enterprise).then(res=>{
					if(res.data.code==20000){
						Toast({title:'保存成功'})
						uni.navigateBack({
							url:'/pageB/enterpriseList/enterpriseList'
						})
					}
				})
			},
			delEnterprise(){
				delEnterprise({'id':this.enterprise.id}).then(res=>{
					if(res.data.code==20000){
						Toast({title:'删除成功'})
						uni.navigateBack({
							url:'/pageB/enterpriseList/enterpriseList'
						})
					}
				})
			}
		},
		onLoad(options) {
			this.enterprise.id = options.id
			this.findById()
			this.findAllNature()
		}
	}
</script>

<style>

</style>
